package com.example.cstr.service.serviceImpl;

import com.example.cstr.entity.Message;
import com.example.cstr.entity.StudyContain;
import com.example.cstr.mapper.StudyContainMapper;
import com.example.cstr.utils.JwtUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Example;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.example.cstr.entity.User;
import com.example.cstr.mapper.UserMapper;
import com.example.cstr.service.UserService;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private StudyContainMapper studyContainMapper;

    @Override
    public int deleteByPrimaryKey(Integer workId) {
        return userMapper.deleteByPrimaryKey(workId);
    }

    @Override
    public int insert(User record) {
        return userMapper.insert(record);
    }

    @Override
    public int insertSelective(User record) {
        return userMapper.insertSelective(record);
    }

    @Override
    public User selectByPrimaryKey(Integer workId) {
        return userMapper.selectByPrimaryKey(workId);
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        return userMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(User record) {
        return userMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<User> selectAllUser() {
        List<User> users = userMapper.selectAllUser();
        for (User user : users) {
            Integer a = studyContainMapper.selectAfterExchangeStudyTimeByScWorkId(user.getWorkId());
            user.setTime(a);
        }
        return users;
    }

    @Override
    public Message Login(Integer workId, String password) {
        User user = userMapper.selectByworkIdAndUserPwd(workId, password);
        if (user != null) {
            return Message.success(null).add(JwtUtil.createJwtToken(user));
        } else return Message.fail(null);
    }

    @Override
    public Message getUserByToken(String token) {
        User user = JwtUtil.getTokenInfo(token);
        if (user != null) {
            return Message.success(null).add(user);
        }
        return Message.fail("token非法");
    }

    @Override
    public Message PagingSearch(@RequestParam Integer pageNum, @RequestParam Integer size) {
        PageHelper.startPage(pageNum, size);
        List<User> userList = userMapper.selectAllUser();
        PageInfo pageInfo = new PageInfo(userList);
        pageInfo.getTotal();
        return Message.success(null).add(pageInfo);
    }

    @Override
    public List<User> sortAllUser() {
        List<User> users = userMapper.sortAllUser();
        int i = 1;
        for (User user : users) {
            Integer a = studyContainMapper.selectAfterExchangeStudyTimeByScWorkId(user.getWorkId());
            user.setTime(a);
            user.setPaiming(i);
            i++;
        }
        return users;
    }

    @Override
    public User sortOwnUser(Integer workId) {
        List<User> users = userMapper.sortAllUser();
        User user11 = null;
        int i = 1;
        for (User user : users) {
            Integer a = studyContainMapper.selectAfterExchangeStudyTimeByScWorkId(user.getWorkId());
            user.setTime(a);
            user.setPaiming(i);
            i++;
        }
        for (User user : users) {
            if (user.getWorkId().equals(workId)) {
                return user;
            }
        }
        return user11;
    }
}







